import pymysql
from dotenv import load_dotenv
import os

# 加载环境变量
load_dotenv()

def get_db_connection():
    """获取数据库连接"""
    db_config = {
        'host': os.getenv('DB_HOST', 'localhost'),
        'port': int(os.getenv('DB_PORT', 3306)),
        'user': os.getenv('DB_USER', 'root'),
        'password': os.getenv('DB_PASSWORD', '123456'),
        'database': os.getenv('DB_NAME', 'zpsjfx'),
        'charset': 'utf8mb4'
    }
    return pymysql.connect(**db_config)

def check_imported_data():
    """检查导入的数据"""
    connection = get_db_connection()
    try:
        with connection.cursor() as cursor:
            # 检查表是否存在
            cursor.execute("SHOW TABLES LIKE 'boss_jobs'")
            result = cursor.fetchone()
            if not result:
                print("表 boss_jobs 不存在")
                return
            
            print("表 boss_jobs 存在")
            
            # 检查记录数
            cursor.execute("SELECT COUNT(*) FROM boss_jobs")
            result = cursor.fetchone()
            if result:
                count = result[0]
                print(f"总记录数: {count}")
            else:
                print("无法获取记录数")
            
            # 查看前几条记录
            cursor.execute("SELECT id, title, salary, companyTitle, address FROM boss_jobs LIMIT 5")
            records = cursor.fetchall()
            print("\n前5条记录:")
            for record in records:
                print(f"  ID: {record[0]}, 职位: {record[1]}, 薪资: {record[2]}, 公司: {record[3]}, 地址: {record[4]}")
            
            # 查看不同职位类型的统计
            cursor.execute("SELECT type, COUNT(*) as count FROM boss_jobs GROUP BY type ORDER BY count DESC")
            types = cursor.fetchall()
            print("\n职位类型统计:")
            for t in types:
                print(f"  {t[0]}: {t[1]}个职位")
                
            # 查看不同薪资范围的统计
            cursor.execute("SELECT salary, COUNT(*) as count FROM boss_jobs GROUP BY salary ORDER BY count DESC LIMIT 10")
            salaries = cursor.fetchall()
            print("\n热门薪资范围统计:")
            for s in salaries:
                print(f"  {s[0]}: {s[1]}个职位")
                
            # 查看不同公司的统计
            cursor.execute("SELECT companyTitle, COUNT(*) as count FROM boss_jobs GROUP BY companyTitle ORDER BY count DESC LIMIT 10")
            companies = cursor.fetchall()
            print("\n招聘职位最多的公司:")
            for c in companies:
                print(f"  {c[0]}: {c[1]}个职位")
                
    except Exception as e:
        print(f"检查数据时出错: {e}")
    finally:
        connection.close()

if __name__ == "__main__":
    check_imported_data()